package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.Car;
import com.vehicle.feign.ParkingFeign;
import com.vehicle.mapper.CarMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.mapper.UserLogMapper;
import com.vehicle.service.CarService;
import com.vehicle.vo.CarList;
import com.vehicle.vo.CarListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-05-14
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Autowired
    CarMapper carMapper;

    @Autowired
    ParkingFeign parkingFeign;

    @Override
    public CarListVO getAllCar(Integer page, Integer size) {
        Page<Car> pageInfo = new Page<>(page,size);
        Page<Car> carPage = carMapper.selectPage(pageInfo,null);
        CarListVO carListVO = new CarListVO();
        carListVO.setSize(carPage.getSize());
        carListVO.setTotal(carPage.getTotal());
        List<Car> cars = carPage.getRecords();
        List<CarList> carLists = new ArrayList<>();
        for (Car c:cars) {
            CarList carList = new CarList();
            BeanUtils.copyProperties(c,carList);
            carList.setCategoryName(parkingFeign.getCategoryNameById(c.getCategoryId()));
            carList.setCreateTime(c.getCreateTime());
            carLists.add(carList);
        }
        carListVO.setContent(carLists);
        return carListVO;
    }

    @Override
    public Boolean UserExist(Integer userId) {
        try{
            QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",userId);
            Car c = carMapper.selectOne(queryWrapper);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public Car getMessageByName(String name) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plate_number",name);
        Car car = carMapper.selectOne(queryWrapper);
        return car;
    }

    @Override
    public Car getMessageByUserId(Integer userId) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        Car car = carMapper.selectOne(queryWrapper);
        return car;
    }

    @Override
    public Integer getCategory(Integer userId) {
       return carMapper.getCategoryId(userId);
    }
}
